package com.tp.test.config;

import io.shardingsphere.api.algorithm.sharding.PreciseShardingValue;
import io.shardingsphere.api.algorithm.sharding.standard.PreciseShardingAlgorithm;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Collection;

/**
 * @author taopeng
 * @version 1.0
 * @Description
 * @date 2021/11/15 17:15:49
 */
@Slf4j
public class TableShardingAlgorithm implements PreciseShardingAlgorithm<String> {
    @Override
    public String doSharding(Collection<String> collection, PreciseShardingValue<String> preciseShardingValue) {
        log.info("TableShardingAlgorithm PreciseShardingAlgorithm<String>");

        collection.stream().forEach((item)->{
            log.info("actual node tables:{}",item);
        });


        log.info("logic table mame : {}  rout column : {}",preciseShardingValue.getLogicTableName(),preciseShardingValue.getColumnName());

        log.info("logic table value : {}",preciseShardingValue.getValue());

        String userId = preciseShardingValue.getValue();

        String tableIndex = StringUtils.substring(userId,4,6);

        for (String item:collection){
            if(StringUtils.equals(item,"user_info_"+tableIndex)){
                return item;
            }
        }
        throw new IllegalArgumentException();
    }
}
